/**
 * Automatic Subtitle Downloader
 * http://code.google.com/p/autosubdown/
 * 
 * Copyright 2010-2011 Raphael Medeiros.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */
package com.byraphaelmedeiros.autosubdown.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.rarfile.FileHeader;

/**
 * @author Raphael Medeiros
 *
 */
public class RarUtils {

	public static void unrarFile(String rarfile, String extdir) {
		File f = new File(rarfile);
		if (null == extdir)
			extdir = f.getParent();
		File ext = new File(extdir);
		if (!ext.exists())
			ext.mkdirs();
		
		try {
			Archive rarFile = new Archive(f);
			
			List<FileHeader> files = rarFile.getFileHeaders();
			
			for (FileHeader fh : files) {
				File fo = new File(extdir + File.separator + fh.getFileNameString());
				
				FileOutputStream fout = new FileOutputStream(fo);
				
				rarFile.extractFile(fh, fout);
				
				fout.close();
			}
		} catch (RarException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
